package com.huawei.appgallery.datastorage.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.hms.framework.network.restclient.hwhttp.trans.FileBinary;
import java.io.File;
import java.io.FileNotFoundException;
import o.bmg;
import o.bmj;
import o.bmk;
import o.bmo;

public class CommonFileProvider extends ContentProvider {
    private static bmk ˋ = new bmj();
    private static final String[] ॱ = new String[]{"_display_name", "_size"};
    private String ˊ;

    public static void ˏ(String str, @NonNull bmo o_bmo) {
        ˋ.ˏ(str, o_bmo);
    }

    @Nullable
    public static Uri ˋ(Context context, @NonNull File file) {
        return ˋ.ˏ(context, file);
    }

    private static String[] ˎ(String[] strArr, int i) {
        Object obj = new String[i];
        System.arraycopy(strArr, 0, obj, 0, i);
        return obj;
    }

    private static Object[] ˊ(Object[] objArr, int i) {
        Object obj = new Object[i];
        System.arraycopy(objArr, 0, obj, 0, i);
        return obj;
    }

    public boolean onCreate() {
        return true;
    }

    public void attachInfo(Context context, ProviderInfo providerInfo) {
        super.attachInfo(context, providerInfo);
        if (providerInfo.exported) {
            throw new SecurityException("WiseDist Provider must not be exported");
        } else if (providerInfo.grantUriPermissions) {
            this.ˊ = providerInfo.authority;
        } else {
            throw new SecurityException("WiseDist Provider must grant uri permissions");
        }
    }

    public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        File ˊ = ˋ.ˊ(uri);
        if (ˊ == null) {
            return null;
        }
        if (strArr == null) {
            strArr = ॱ;
        }
        String[] strArr3 = new String[strArr.length];
        Object[] objArr = new Object[strArr.length];
        int length = strArr.length;
        int i = 0;
        int i2 = 0;
        while (i < length) {
            int i3;
            Object obj = strArr[i];
            if ("_display_name".equals(obj)) {
                strArr3[i2] = "_display_name";
                i3 = i2 + 1;
                objArr[i2] = ˊ.getName();
            } else if ("_size".equals(obj)) {
                strArr3[i2] = "_size";
                i3 = i2 + 1;
                objArr[i2] = Long.valueOf(ˊ.length());
            } else {
                i3 = i2;
            }
            i++;
            i2 = i3;
        }
        String[] ˎ = ˎ(strArr3, i2);
        Object[] ˊ2 = ˊ(objArr, i2);
        Cursor matrixCursor = new MatrixCursor(ˎ, 1);
        matrixCursor.addRow(ˊ2);
        return matrixCursor;
    }

    public String getType(Uri uri) {
        if (!(uri == null || TextUtils.isEmpty(uri.toString()))) {
            String mimeTypeFromExtension;
            if (uri.toString().endsWith("apk")) {
                mimeTypeFromExtension = MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk");
                if (mimeTypeFromExtension != null) {
                    return mimeTypeFromExtension;
                }
            } else if (uri.toString().endsWith("zip")) {
                mimeTypeFromExtension = MimeTypeMap.getSingleton().getMimeTypeFromExtension("zip");
                if (mimeTypeFromExtension != null) {
                    return mimeTypeFromExtension;
                }
            }
        }
        return FileBinary.HEAD_VALUE_CONTENT_TYPE_OCTET_STREAM;
    }

    public Uri insert(Uri uri, ContentValues contentValues) {
        bmg.ˊ.ˏ("CommonFileProvider", "unsupport insert");
        return null;
    }

    public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
        bmg.ˊ.ˏ("CommonFileProvider", "unsupport update");
        return -1;
    }

    public int delete(Uri uri, String str, String[] strArr) {
        bmg.ˊ.ˏ("CommonFileProvider", "unsupport delete");
        return 0;
    }

    public ParcelFileDescriptor openFile(Uri uri, String str) throws FileNotFoundException {
        if (uri == null || uri.getAuthority() == null || !uri.getAuthority().equals(this.ˊ)) {
            return null;
        }
        File ˊ = ˋ.ˊ(uri);
        if (ˊ == null) {
            return null;
        }
        if (ˊ.getName().endsWith("apk") || ˊ.getName().endsWith("zip")) {
            return ParcelFileDescriptor.open(ˊ, 268435456);
        }
        bmg.ˊ.ˏ("CommonFileProvider", "can not open this file.");
        return null;
    }
}
